package com.zy.openchat.core.model.circle;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;

import java.util.ArrayList;

public class UserTags implements Parcelable {
    public boolean isChecked = false;
    public long id;
    @SerializedName(value = "title")
    public String name;
    public ArrayList<UserInfo> userInfoLis;
    public ArrayList<Long> users;

    public UserTags(String name, ArrayList<UserInfo> userInfoLis) {
        this.name = name;
        this.userInfoLis = userInfoLis;
        users = new ArrayList<>();
        if(!Common.isEmpty(userInfoLis)){
            for(UserInfo userInfo:userInfoLis){
                users.add(userInfo.id);
            }
        }
    }

    public boolean isEqualUsers(UserTags userTags){
        if(userTags==null){
            return false;
        }
        if(Common.isEmpty(users)&&Common.isEmpty(userTags.users)){
            return true;
        }
        if(Common.isEmpty(users)||Common.isEmpty(userTags.users)){
            return false;
        }
        if(users.size()!=userTags.users.size()){
            return false;
        }
        for(long userId:users){
            boolean isFindEqual = false;
            for(long findUserId:userTags.users){
                if(findUserId == userId){
                    isFindEqual = true;
                    break;
                }
            }
            if(!isFindEqual){
                return false;
            }
        }
        return true;
    }

    public boolean isEqualName(UserTags userTags){
        if(userTags==null){
            return false;
        }
        if(TextUtils.isEmpty(name)&& TextUtils.isEmpty(userTags.name)){
            return true;
        }
        if(TextUtils.isEmpty(name)|| TextUtils.isEmpty(userTags.name)){
            return false;
        }
        return name.equals(userTags.name);
    }

    public void copy(UserTags userTags){
        if(userTags==null){
            return;
        }
        name = userTags.name;
        userInfoLis = userTags.userInfoLis;
        users = userTags.users;
    }

    public UserTags copy(){
        UserTags userTags = new UserTags(name,userInfoLis);
        userTags.users = users;
        userTags.id = id;
        userTags.isChecked = false;
        return userTags;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeString(this.name);
        dest.writeTypedList(this.userInfoLis);
        dest.writeList(this.users);
    }

    public void readFromParcel(Parcel source) {
        this.id = source.readLong();
        this.name = source.readString();
        this.userInfoLis = source.createTypedArrayList(UserInfo.CREATOR);
        this.users = new ArrayList<Long>();
        source.readList(this.users, Long.class.getClassLoader());
    }

    protected UserTags(Parcel in) {
        this.id = in.readLong();
        this.name = in.readString();
        this.userInfoLis = in.createTypedArrayList(UserInfo.CREATOR);
        this.users = new ArrayList<Long>();
        in.readList(this.users, Long.class.getClassLoader());
    }

    public static final Creator<UserTags> CREATOR = new Creator<UserTags>() {
        @Override
        public UserTags createFromParcel(Parcel source) {
            return new UserTags(source);
        }

        @Override
        public UserTags[] newArray(int size) {
            return new UserTags[size];
        }
    };
}
